package com.SoilMM.controller;


import com.SoilMM.entity.AdminEntity;
import com.SoilMM.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gramorgan
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;


    @RequestMapping("doLogin")
    public Map<String,Object> doLogin(@RequestBody AdminEntity adminEntity, HttpSession session){
        Map<String,Object> map=new HashMap<>();

        if (adminService.doLogin(adminEntity)) {
            session.setAttribute("loginUser",adminEntity);
            map.put("message","登录成功");
        }
        else {
            map.put("message","登录失败");
        }

        return map;
    }

    @RequestMapping("doRegister")
    public Map<String,Object> doRegister(@RequestBody AdminEntity adminEntity)
    {
        Map<String,Object> map=new HashMap<>();
        if (adminService.save(adminEntity))
        {
            map.put("message","注册成功");
        }
        else {
            map.put("message","注册失败");
        }
        return map;
    }



}

